package cn.wolfcode.ssm.controller;

import cn.wolfcode.ssm.qo.JsonResult;
import cn.wolfcode.ssm.service.IEmployeeService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {

    @Autowired
    private IEmployeeService employeeService;

    //username,password其实是用户传过来的参数
    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password) {
        try {
            //创建一个令牌,里面包含用户填写的账号和密码
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            //执行登录操作
            SecurityUtils.getSubject().login(token);
            return new JsonResult();
        } catch (UnknownAccountException e) {
            return new JsonResult(false, "账号不存在");
        } catch (IncorrectCredentialsException e) {
            return new JsonResult(false, "密码错误");
        } catch (DisabledAccountException e){
            return  new JsonResult(false,"账号被冻结,请联系管理员");
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "登录异常，请联系管理员");
        }
    }

}
